package com.avengers.modules.epp.bean.emums;

import com.avengers.core.bean.enums.RegistrarCode;
import com.avengers.modules.auction.bean.enums.Suffix;
import com.avengers.core.bean.enums.SymbolEnum;
import com.avengers.core.kit.DomainKit;
import com.avengers.core.kit.StringKit;

import java.util.Arrays;
import java.util.List;

/**
 * @author yu.wang
 * @since 2019-07-10 17:35
 **/
public enum EppType {
    // epp type
    all(""),
    com_net(Suffix.com.value, Suffix.net.value),
    cc_tv(Suffix.cc.value, Suffix.tv.value),
    org(Suffix.org.value),
    in(Suffix.in.value),
    info(Suffix.info.value),
    cnnic_common(Suffix.cn.value, Suffix.cnZh.value),
    cnnic_ng(".公司", ".网络", ".佛山", ".广东"),
    zdns(".top", ".wang", ".ren", ".网址", ".集团", ".我爱你", ".新闻", ".时尚", ".慈善"),
    other;

    private final List<String> suffixList;

    EppType(String... suffix) {
        this.suffixList = Arrays.asList(suffix);
    }

    public static EppType by(String domain, RegistrarCode registrar) {
        if (null != registrar && !registrar.isOwn) {
            return all;
        }
        return of(domain);
    }

    public static EppType of(String domain) {
        if (StringKit.isBlank(domain)) {
            return null;
        }
        String fDomain;
        try {
            fDomain = DomainKit.fromPunyCode(domain);
        } catch (Exception e) {
            fDomain = domain;
        }
        String suffix = fDomain.substring(fDomain.lastIndexOf(SymbolEnum.dot.string)).toLowerCase();
        for (EppType type : values()) {
            if (type.suffixList.contains(suffix)) {
                return type;
            }
        }
        return other;
    }

    public static boolean isSupported(String eppType) {
        return null != format(eppType);
    }

    public static EppType format(String eppType) {
        if (StringKit.isBlank(eppType)) {
            return null;
        }
        for (EppType type : EppType.values()) {
            if (type.name().equalsIgnoreCase(eppType)) {
                return type;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return this.name();
    }
}
